package com.example.maze;

import java.util.ArrayList;
import java.util.List;

import com.example.maze.DatabaseHandler.LeaderBoardTable;
import com.example.maze.DatabaseHandler.ScoreTable;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class LeaderBoardDataSource {

	private SQLiteDatabase database;
	private DatabaseHandler dbHelper;

	//setting up the data base and relevant methods
	public LeaderBoardDataSource(Context context) {
		dbHelper = new DatabaseHandler(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}

	public void close() {
		dbHelper.close();
	}

	public void addPlayerScore(PlayerScore playerScore) {
		//getting the score details and putting them into the appropriate columns 
		//in the score table
		ContentValues scoreValues = new ContentValues();
		Score score = playerScore.getScore();
		scoreValues.put(ScoreTable.COLUMN_NAME_MOVES, score.getMoves());
		scoreValues.put(ScoreTable.COLUMN_NAME_PERCENT, score.getPercent());
		scoreValues.put(ScoreTable.COLUMN_NAME_SCORE, score.getScore());
		scoreValues.put(ScoreTable.COLUMN_NAME_SIZE, score.getSize());
		scoreValues.put(ScoreTable.COLUMN_NAME_TIME, score.getTime());
		scoreValues.put(ScoreTable.COLUMN_NAME_TYPE, score.getType().ordinal());
		//inserting them into the columns
		long scoreId = database
				.insert(ScoreTable.TABLE_NAME, null, scoreValues);
		//putting the data into the leader board table
		ContentValues playerScoreValues = new ContentValues();
		playerScoreValues.put(LeaderBoardTable.COLUMN_NAME_NAME,
				playerScore.getName());
		playerScoreValues.put(LeaderBoardTable.COLUMN_NAME_SCORE_ID, scoreId);
		database.insert(LeaderBoardTable.TABLE_NAME, null, playerScoreValues);
	}
	
	 public List<PlayerScore> getLeaderBoard(int minScore, int maxScore) {
		    List<PlayerScore> leaderBoard = new ArrayList<PlayerScore>();
		    //getting the data from tables 
		    //and putting it in the leader board with an inner join 
		    //also only giving the data to the right medal 
		    //by sorting it with a min and max score and a descending order so best scores at the top
		    String sql = "Select " 
		    		+ LeaderBoardTable.COLUMN_NAME_NAME + " , "
		    		+ ScoreTable.COLUMN_NAME_MOVES + " , "
		    		+ ScoreTable.COLUMN_NAME_TIME + " , "
		    		+ ScoreTable.COLUMN_NAME_SIZE + " , "
		    		+ ScoreTable.COLUMN_NAME_PERCENT + " , "
		    		+ ScoreTable.COLUMN_NAME_TYPE
		    		+ " From " + LeaderBoardTable.TABLE_NAME + " Inner Join " 
		    		+ ScoreTable.TABLE_NAME + " On " + LeaderBoardTable.COLUMN_NAME_SCORE_ID 
		    		+ " = " + ScoreTable.TABLE_NAME + "." + ScoreTable._ID 
		    		+ " Where " + ScoreTable.COLUMN_NAME_SCORE + " >= " + minScore 
		    		+ " AND " + ScoreTable.COLUMN_NAME_SCORE + " < " + maxScore
		    		+ " Order By " + ScoreTable.COLUMN_NAME_SCORE + " DESC";
		    
		    //making cursor to go through the data
			String[] arr = new String[0];
			Cursor cursor = database.rawQuery(sql, arr );

		    cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	//telling cursor to start at the start and add in right order
		      PlayerScore playerScore = cursorToPlayerScore(cursor);
		      leaderBoard.add(playerScore);
		      cursor.moveToNext();
		    }
		    // make sure to close the cursor
		    cursor.close();
		    return leaderBoard;
		  }

		  private PlayerScore cursorToPlayerScore(Cursor cursor) {
			  //getting the data
		    String name = cursor.getString(0);
		    int moves = cursor.getInt(1);
		    int time = cursor.getInt(2);
		    int size = cursor.getInt(3);
		    int percent = cursor.getInt(4);
		    int type = cursor.getInt(5);
		    //getting the modes and finding which mode was picked
		    GameMode gameMode = GameMode.values()[type];
		    //putting all the data in a safe place and returning it
		    Score formingScore = new Score(size, percent, gameMode, time, moves);
		    PlayerScore playerScore = new PlayerScore(name, formingScore);
		    return playerScore;
		  }
}
